package com.ht.pms.core.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ht.pms.core.dao.SalaryTemplateDetailDao;
import com.ht.pms.core.dto.dict.DictParamsDto;
import com.ht.pms.core.dto.tempalte.TemplateDetail;
import com.ht.pms.core.dto.tempalte.TemplateDetailDto;

@Repository
public class SalaryTemplateDetailDaoImpl implements SalaryTemplateDetailDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<TemplateDetailDto> selectByTemplateCode(String templateCode) {
		String sql = "select a.* , b.dict_name from t_template_detail a left join t_dict_params b on a.dict_code = b.dict_code  where a.template_code = ? ORDER BY a.sort asc";

		return jdbcTemplate.query(sql, new Object[] { templateCode },
				new BeanPropertyRowMapper<TemplateDetailDto>(TemplateDetailDto.class));
	}

	@Override
	public List<DictParamsDto> selectDictParamsByTemplateCode(String templateCode) {
		String sql = null;
		if(StringUtils.isNotEmpty(templateCode)) {
			
		}
		 sql = "select a.is_view, a.sort, a.is_must , b.* from t_template_detail a left join t_dict_params b on a.dict_code = b.dict_code  where a.template_code = ? ORDER BY a.sort asc";

		return jdbcTemplate.query(sql, new Object[] { templateCode },
				new BeanPropertyRowMapper<DictParamsDto>(DictParamsDto.class));
	}

	@Override
	public void deleteByDictCodeList(List<String> resultFields, String templateCode) {
		StringBuilder sql = new StringBuilder();
		for (String value : resultFields) {
			if(sql.length() == 0) {
				sql.append("delete from t_template_detail where template_code = ? and dict_code not in ('").append(value).append("'");
			}else {
				sql.append(",'").append(value).append("'");
			}
		}
		sql.append(")");
		jdbcTemplate.update(sql.toString(), templateCode);
	}

	@Override
	public void insertBatchByTemplateCode(List<TemplateDetail> insertParam) {
		String sql = "insert into t_template_detail(dict_code, template_code, sort, is_view, is_must) values(?, ?, ?, ?, ?)";
		jdbcTemplate.batchUpdate(sql , new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setString(1, insertParam.get(i).getDictCode());
				ps.setString(2, insertParam.get(i).getTemplateCode());
				ps.setInt(3, insertParam.get(i).getSort());
				ps.setInt(4, insertParam.get(i).getIsView());
				ps.setInt(5, insertParam.get(i).getIsMust());
			}

			@Override
			public int getBatchSize() {
				return insertParam.size();
			}
			
		});
	}

	@Override
	public void updateSortBatchByTemplateCode(List<TemplateDetail> updateParam) {
		String sql = "update t_template_detail set sort = ? where template_code = ? and dict_code = ?";
		jdbcTemplate.batchUpdate(sql , new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, updateParam.get(i).getSort());
				ps.setString(2, updateParam.get(i).getTemplateCode());
				ps.setString(3, updateParam.get(i).getDictCode());
			}

			@Override
			public int getBatchSize() {
				return updateParam.size();
			}
			
		});
	}

	@Override
	public void updateBatchByTemplateCode(List<TemplateDetail> templateDetails) {
		String sql = "update t_template_detail set is_view = ? , is_must = ?, sort = ? where template_code = ? and dict_code = ?";
		jdbcTemplate.batchUpdate(sql , new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, templateDetails.get(i).getIsView());
				ps.setInt(2, templateDetails.get(i).getIsMust());
				ps.setInt(3, templateDetails.get(i).getSort());
				ps.setString(4, templateDetails.get(i).getTemplateCode());
				ps.setString(5, templateDetails.get(i).getDictCode());
			}

			@Override
			public int getBatchSize() {
				return templateDetails.size();
			}
		});
		
	}
	
}
